package cn.edu.buaa.nlsde.grid.Chart;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;

import cn.edu.buaa.nlsde.grid.DatabaseConnect.*;
import cn.edu.buaa.nlsde.grid.DatabaseManage.*;

public class LoadChart
{
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private final int MAX=800;
	private long time1=System.currentTimeMillis();
	private Date date1=new Date(time1);
	private String currentTime=sdf.format(date1);
	
	private long time2=time1-60*60*1000;
	private Date date2=new Date(time2);
	private String lastTime=sdf.format(date2);
	private DBPool dbpool;
	
	public LoadChart() throws Exception
	{
		dbpool=new DBPool(DBInitial.getJndiName());
	}

	public List<List<AxisBuild>> getLoadAxis(int HostID)
	{
		MetricLoadManage load=new MetricLoadManage(dbpool);
		ResultSet rs=load.getData(HostID,lastTime,currentTime);
		List<List<AxisBuild>> list = new ArrayList<List<AxisBuild>>();
		AxisBuild chart=null;
		try
		{
			int i=0;
			while(rs.next()&&i<MAX)
			{
				Date date=sdf.parse(rs.getString("CollectTime"));
				long time=date.getTime();
				float xAxis=(float)(time-time2)*(float)720/(float)(time1-time2)+(float)100;
				float yAxis;
				List<AxisBuild> temp=new ArrayList<AxisBuild>();
				
				chart=new AxisBuild();
				chart.setMetric("load_one");
				yAxis=(float)350-(float)1000*rs.getFloat("load_one");
				chart.setxAxis(xAxis);
				chart.setyAxis(yAxis);
				temp.add(chart);
				
				chart=new AxisBuild();
				chart.setMetric("load_five");
				yAxis=(float)350-(float)1000*rs.getFloat("load_five");
				chart.setxAxis(xAxis);
				chart.setyAxis(yAxis);
				temp.add(chart);
				
				chart=new AxisBuild();
				chart.setMetric("load_fifteen");
				yAxis=(float)350-(float)1000*rs.getFloat("load_fifteen");
				chart.setxAxis(xAxis);
				chart.setyAxis(yAxis);
				temp.add(chart);
				
				list.add(temp);
			}
			dbpool.close();
			rs.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
		
		return list;
	}

	public List<AxisBuild> getNewLoadAxis(int HostID)
	{
		MetricLoadManage load=new MetricLoadManage(dbpool);
		ResultSet rs=load.getData(HostID);
		List<AxisBuild> list = new ArrayList<AxisBuild>();
		AxisBuild chart=null;
		try
		{
			if(rs.next())
			{
				String collectTime=rs.getString("CollectTime");
				Date date=sdf.parse(collectTime);
				long time=date.getTime();
				float xAxis=(float)(time-time2)*(float)720/(float)(time1-time2)+(float)100;
				float yAxis;
				
				chart=new AxisBuild();
				chart.setMetric("load_one");
				yAxis=(float)350-(float)1000*rs.getFloat("load_one");
				chart.setxAxis(xAxis);
				chart.setyAxis(yAxis);
				chart.setCollectTime(collectTime);
				list.add(chart);
				
				chart=new AxisBuild();
				chart.setMetric("load_five");
				yAxis=(float)350-(float)1000*rs.getFloat("load_five");
				chart.setxAxis(xAxis);
				chart.setyAxis(yAxis);
				chart.setCollectTime(collectTime);
				list.add(chart);
				
				chart=new AxisBuild();
				chart.setMetric("load_fifteen");
				yAxis=(float)350-(float)1000*rs.getFloat("load_fifteen");
				chart.setxAxis(xAxis);
				chart.setyAxis(yAxis);
				chart.setCollectTime(collectTime);
				list.add(chart);
			}
			dbpool.close();
			rs.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
		
		return list;
	}
	
	public void close() throws SQLException
	{
		dbpool.close();
	}
}